import sys

if len(sys.argv) < 4:
    print 'Usage:<test.group> <new_index.txt> <KDD_Track1_solution.csv> <test.group.rate>'

idmap = {}
for l in open( sys.argv[2] ):
    arr = l.split()
    idmap[ int(arr[0]) ] = int(arr[2])

imap = {}
fi = open( sys.argv[3] )
fi.readline()
for l in fi:
    arr = l.split(',')
    uid   = idmap[ int(arr[0]) ]
    for it in arr[1].split():
        iid = idmap[ int(it) ]
        if iid not in imap:
            imap[ iid ] = {}        
        assert uid not in imap[ iid ]
        imap[ iid ][ uid ] = 1


fo = open( sys.argv[4], 'w' )
for l in open( sys.argv[1] ):
    arr = l.split()
    uid = int(arr[0])
    iid = int(arr[1])

    tstamp = long( arr[3] ) 
    if tstamp < 1321891200:
        ispr = 0
    else:
        ispr = 1

    if (not iid in imap) or (not uid in imap[iid]):
        fo.write( '%d\t0\n' % ispr )
    else:
        fo.write( '%d\t1\n' % ispr )
fo.close()
